
using IGeekFan.AspNetCore.Knife4jUI;
using NLog;
using NLog.Web;
using RBAC.Api.Infaction;
using RBAC.Api.Writh.Extensions;
using System.Reflection;

// 初始化NLog
var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("初始化 RBAC.Api.Writh");

try
{
    var builder = WebApplication.CreateBuilder(args);
    
    // 添加NLog
    builder.Logging.ClearProviders();
    builder.Host.UseNLog();
    
    builder = builder.Inject();

//// Add services to the container.

//builder.Services.AddControllers();
//// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
//builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();

////ע��������
//builder.Services.AddDbContext<MyDbContext>();

////ע��ִ��ӿ�
//builder.Services.AddScoped(typeof(IBaseRepositry<>), typeof(BaseRepositry<>));

////ע���н���
//builder.Services.AddMediatR(x =>
//{
//    x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
//});

var app = builder.Build();

// Configure the HTTP request pipeline.

    app.UseSwagger();
    // app.UseSwaggerUI();

    app.UseAuthentication(); // 认证

    app.UseAuthorization(); // 再授权

    app.UseKnife4UI();

//����
app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());

app.MapControllers();

app.UseStaticFiles(); // 启用静态文件服务

    app.Run();
}
catch (Exception ex)
{
    // 记录启动过程中的错误
    logger.Error(ex, "程序启动失败");
    throw;
}
finally
{
    // 确保在应用程序关闭时正确关闭NLog
    NLog.LogManager.Shutdown();
}
